const express = require('express');
const pool = require('../pool.js');
const router = express.Router();
//用户注册
router.post('/reg',(req,res,next)=>{
	//res.send({code:200,msg:'注册成功'});
	let obj = req.body;
	//res.send(obj);
	if(!obj.uname){
		res.json({code:401,msg:'uname不能为空'});
		//阻止往后执行
		return ;
	}
	if(!obj.upwd){
		res.send({code:402,msg:'upwd不能为空'});
		return ;
	}
	if(!obj.email){
		res.send({code:403,msg:'email不能为空'});
		return ;
	}
	if(!obj.phone){
		res.send({code:404,msg:'phone不能为空'});
		return ;
	}
	//验证手机号码格式
	//正则表达式.test(手机号码)
	//如果手机号码格式不正确
	if(!/^1[3-9]\d{9}$/.test(obj.phone)){
		res.send({code:405,msg:'手机号码格式不正确'});
		return ;
	}
	pool.query('insert into xz_user set ?',[obj],(err,result)=>{
		if(err){
			//如果数据库执行出现了错误,交给下一个错误处理的中间件
			next(err);
			return ;
		} 
		if(result.affectedRows>0){
			res.send({code:200,msg:'reg suc'});
		}else{
			res.send({code:201,msg:'reg err'});
		}
	});
});
//用户登录
router.post('/login',(req,res,next)=>{
	let obj = req.body;
	console.log(obj)
	if(!obj.uname){
		res.send({code:401,msg:'uname不能为空'});
		return ;
	}
	if(!obj.upwd){
		res.send({code:402,msg:'upwd不能为空'});
		return ;
	}
	pool.query('select * from xz_user where uname=? and upwd=?',[obj.uname,obj.upwd],(err,result)=>{
			if(err){
				next(err);
				return;
			} 
			if(result.length>0){
				res.send({code:200,msg:'login suc',data:result});
			}else{
				res.send({code:201,msg:'login err'});
			}
	});
});
//3.修改用户
router.put('/',(req,res,next)=>{
	
	//3.1获取post传递的数据(uid,email,phone,user_name,gender)
	let obj = req.body;
	console.log(obj);
	//验证数据数据是否为空
	let i=400;
	for(var key in obj){
		//key 属性名 obj[key]属性值
		//console.log(key,obj[key]);
		//每遍历一次属性，状态码加1
		i++;
		if(!obj[key]){
			res.send({code:i,msg:key+'不能为空'});
			return ;
		}
	}
	
	//执行SQL命令
	pool.query('update xz_user set ? where uid=?',[obj,obj.uid],(err,result)=>{
		if(err){
			next(err);
			return ;
		}
		if(result.affectedRows === 0){
			res.send({code:201,msg:'update err'});
		}else{
			res.send({code:200,msg:'update suc'})
		}
	});
	
});
//4.查找员工
router.get('/:uid',(req,res,next)=>{
	let obj = req.params;
	pool.query('select * from xz_user where uid=?',[obj.uid],(err,result)=>{
		if(err){
			next(err);
			return ;
		}
		//console.log(result);
		//res.send('成功');
		
		if(result.length>0){
			res.send({code:200,msg:'select成功',data:result});
		}else{
			res.send({code:201,msg:'select失败'});		
		}
		
	});
});
//5.删除员工
router.delete('/:uid',(req,res,next)=>{
	let obj = req.params
	//console.log(obj);
	pool.query('delete from xz_user where uid=?',[obj.uid],(err,result)=>{
		if(err){
			next(err);
			return ;
		}
		if(result.affectedRows>0){
			res.send({code:200,msg:'删除成功'});
		}else{
			res.send({code:201,msg:'删除失败'});		
		}
	});
});
//6.分页查询
router.get('/',(req,res,next)=>{
		let obj  = req.query;
		//console.log(obj);
		//pool.query("");
		if(!obj.pon){obj.pno=1;}
		if(!obj.count){obj.count=3;}
		//console.log(obj);
		obj.count = parseInt(obj.count);
		let start = (obj.pno-1)*obj.count;
		console.log(obj.count,start);
		
		pool.query('select * from xz_user limit ?,? ',[start,obj.count],(err,result)=>{
			if(err){ 
				next(err);
				return ;
			}
			console.log(result);
			res.send({code:200,msg:"select suc",data:result});
		});
		
		
});
module.exports = router;